package cn.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * @auth wzy
 * @date 2023/9/27 15:32
 */
public class Test1 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse("resources/phone.xml");

        NodeList brands = doc.getElementsByTagName("brand");

        for (int i = 0; i <brands.getLength(); i++) {
            Node node = brands.item(i);
            Element element = (Element) node;

            String nameVal = element.getAttribute("name");
            System.out.println("nameVal = " + nameVal);

            NodeList childNodes = element.getChildNodes();
            for (int j = 0; j < childNodes.getLength();j++){
                System.out.println("childNodes.item(j).getNodeName() = " + childNodes.item(j).getNodeName());
//                if (childNodes.item(j).getNodeName().equals("#text")){
//                    continue;
//                }
                if (childNodes.item(j).getNodeType() != Node.ELEMENT_NODE){
                    continue;
                }


                Element elementChild = (Element)childNodes.item(j);
                String nameType = elementChild.getAttribute("name");
                System.out.println("nameType = " + nameType);
            }
        }
    }
}
